﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;

public partial class admin_Gallerys_Detail : System.Web.UI.Page
{
    CateGalleryBLL cate = new CateGalleryBLL();
    GalleryBLL gallery = new GalleryBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["url_back_admin"] = Request.RawUrl;
            LoadTitle();
            LoadCategory();
            if (Request.QueryString["Id"] != null)
            {
                Filldata();
                dropParent.Enabled = false;
            }
        }
    }

    private void LoadCategory()
    {
        ListItem itemRoot = new ListItem("Danh mục gốc", "0");
        dropParent.Items.Insert(0, itemRoot);
        List<CateGallery> listDataSource = cate.GetAll();

        foreach (CateGallery item in listDataSource)
        {
            ListItem obj = new ListItem("--" + item.Name, item.Id.ToString());
            dropParent.Items.Add(obj);
        }
    }

    private void Filldata()
    {
        int id = Convert.ToInt32(Request.QueryString["Id"]);
        Gallery data = gallery.GetById(id);
        txtName.Text = data.Name;
        // txtDescription.Text = product.Description;

        dropParent.SelectedValue = data.CateGalleryId.ToString();
        if (data.IsEnable == true) radioEnable.Checked = true;
        else radioVisible.Checked = true;

        //Load Image
        if (data.Image_1 != null)
        {
            image_01.ImageUrl = "~/Images/Gallery/" + data.Image_1;
            image_01.Visible = true;
            check_Del_img1.Visible = true;
        }
        if (data.Image_2 != null)
        {
            image_02.ImageUrl = "~/Images/Gallery/" + data.Image_2;
            image_02.Visible = true;
            check_Del_img2.Visible = true;
        }
        if (data.Image_3 != null)
        {
            image_03.ImageUrl = "~/Images/Gallery/" + data.Image_3;
            image_03.Visible = true;
            check_Del_img3.Visible = true;
        }
        if (data.Image_4 != null)
        {
            image_04.ImageUrl = "~/Images/Gallery/" + data.Image_4;
            image_04.Visible = true;
            check_Del_img4.Visible = true;
        }
        if (data.Image_5 != null)
        {
            image_05.ImageUrl = "~/Images/Gallery/" + data.Image_5;
            image_05.Visible = true;
            check_Del_img5.Visible = true;
        }
        if (data.Image_6 != null)
        {
            image_06.ImageUrl = "~/Images/Gallery/" + data.Image_6;
            image_06.Visible = true;
            check_Del_img6.Visible = true;
        }
    }

    private void LoadTitle()
    {
        if (Request.QueryString["Id"] != null)
        {
            h3Title.InnerText = "Chỉnh sửa thông tin hình ảnh";
        }
    }
    private bool isValidate()
    {
        if (txtName.Text == "")
        {
            Myfunction.ThongBao(lbThongBao, "Tên ảnh không được để trống !");
            txtName.Focus();
            return false;
        }
        if (dropParent.SelectedIndex == -1)
        {
            Myfunction.ThongBao(lbThongBao, "Chọn danh mục thư viện ảnh!");
            return false;
        }
        return true;
    }
    private void ResetForm()
    {
        txtName.Text = "";
        radioEnable.Checked = true;
    }

    private void Update()
    {
        int id = Convert.ToInt32(Request.QueryString["Id"]);
        Gallery data = gallery.GetById(id);

        data.CateGalleryId = Convert.ToInt32(dropParent.SelectedValue);
        data.Name = txtName.Text;
        data.IsEnable = (radioEnable.Checked ? true : false);

        //Copy image phu
        if (fileUploadImage_01.HasFile && !check_Del_img1.Checked)
        {
            data.Image_1 = Myfunction.CopyFile(fileUploadImage_01.PostedFile, "Images/Gallery", data.Name + "-01");
        }
        if (fileUploadImage_02.HasFile && !check_Del_img2.Checked)
        {
            data.Image_2 = Myfunction.CopyFile(fileUploadImage_02.PostedFile, "Images/Gallery", data.Name + "-02");
        }
        if (fileUploadImage_03.HasFile && !check_Del_img3.Checked)
        {
            data.Image_3 = Myfunction.CopyFile(fileUploadImage_03.PostedFile, "Images/Gallery", data.Name + "-03");
        }
        if (fileUploadImage_04.HasFile && !check_Del_img4.Checked)
        {
            data.Image_4 = Myfunction.CopyFile(fileUploadImage_04.PostedFile, "Images/Gallery", data.Name + "-04");
        }
        if (fileUploadImage_05.HasFile && !check_Del_img5.Checked)
        {
            data.Image_5 = Myfunction.CopyFile(fileUploadImage_05.PostedFile, "Images/Gallery", data.Name + "-05");
        }
        if (fileUploadImage_06.HasFile && !check_Del_img6.Checked)
        {
            data.Image_6 = Myfunction.CopyFile(fileUploadImage_06.PostedFile, "Images/Gallery", data.Name + "-06");
        }
        //Delete image
        if (check_Del_img1.Checked)
        {
            Myfunction.DeleteFile("Images/Gallery", data.Image_1);
            data.Image_1 = null;
        }
        if (check_Del_img2.Checked)
        {
            Myfunction.DeleteFile("Images/Gallery", data.Image_2);
            data.Image_2 = null;
        }
        if (check_Del_img3.Checked)
        {
            Myfunction.DeleteFile("Images/Gallery", data.Image_3);
            data.Image_3 = null;
        }
        if (check_Del_img4.Checked)
        {
            Myfunction.DeleteFile("Images/Gallery", data.Image_4);
            data.Image_4 = null;
        }
        if (check_Del_img5.Checked)
        {
            Myfunction.DeleteFile("Images/Gallery", data.Image_5);
            data.Image_5 = null;
        }
        if (check_Del_img6.Checked)
        {
            Myfunction.DeleteFile("Images/Gallery", data.Image_6);
            data.Image_6 = null;
        }
        gallery.Update(data);

        Myfunction.ThongBao(lbThongBao, "Thông tin hình ảnh đã được cập nhật");
        Filldata();
    }
    private void Add()
    {
        Gallery data = new Gallery();

        data.CateGalleryId = Convert.ToInt32(dropParent.SelectedValue);
        data.Name = txtName.Text;
        data.IsEnable = (radioEnable.Checked ? true : false);
        data.CreateDate = DateTime.Now;
        //Copy image phu
        if (fileUploadImage_01.HasFile)
        {
            data.Image_1 = Myfunction.CopyFile(fileUploadImage_01.PostedFile, "Images/Gallery", data.Name + "-01");
        }
        if (fileUploadImage_02.HasFile)
        {
            data.Image_2 = Myfunction.CopyFile(fileUploadImage_02.PostedFile, "Images/Gallery", data.Name + "-02");
        }
        if (fileUploadImage_03.HasFile)
        {
            data.Image_3 = Myfunction.CopyFile(fileUploadImage_03.PostedFile, "Images/Gallery", data.Name + "-03");
        }
        if (fileUploadImage_04.HasFile)
        {
            data.Image_4 = Myfunction.CopyFile(fileUploadImage_04.PostedFile, "Images/Gallery", data.Name + "-04");
        }
        if (fileUploadImage_05.HasFile)
        {
            data.Image_5 = Myfunction.CopyFile(fileUploadImage_05.PostedFile, "Images/Gallery", data.Name + "-05");
        }
        if (fileUploadImage_06.HasFile)
        {
            data.Image_6 = Myfunction.CopyFile(fileUploadImage_06.PostedFile, "Images/Gallery", data.Name + "-06");
        }
        gallery.Add(data);
        Myfunction.ThongBao(lbThongBao, "Thông tin hình ảnh đã được cập nhật !");
    }
    protected void btnOk_Click(object sender, EventArgs e)
    {
        if ((int)Session["Role"] != 1)
            {
              Myfunction.ThongBao(lbThongBao, "Bạn không có quyền thực hiện thao tác này ! ");
              return;
            }
            try
            {
              lbThongBao.Text = "";
              if (isValidate())
              {
                if (Request.QueryString["Id"] == null)
                {
                  Add();
                  ResetForm();
                }
                else
                  Update();

              }
            }
            catch (Exception ex)
            {
              Myfunction.ThongBao(lbThongBao, ex.Message);
            }
    }
}